<?php
/**
 * Created by PhpStorm
 * User: 龍zero
 * Date: 2022-04-21
 * Time: 9:45
 */
declare(strict_types=1);
namespace App\Service;

use App\Model\Book;
use Hyperf\HttpServer\Annotation\Controller;

class BookService
{
    public function createBook($params):void
    {
        $book = new Book();
        $book->author = $params['author'];
        $book->title = $params['title'];
        $book->image = $params['image'];
        $book->summary = $params['summary'];
        $book->save();
    }

    public function updateBook($params):void
    {
        $book = Book::find($params['id']);
        $book->author = $params['author'];
        $book->title = $params['title'];
        $book->image = $params['image'];
        $book->summary = $params['summary'];
        $book->save();
    }

    public function deleteById($id):void
    {
        $book = Book::find($id);
        $book->delete();
    }

    public function getBookByKeyword($q){
        return Book::where('title','like','%'.$q.'%')->get()->toArray();
    }


    public function getById($id){
        return Book::find($id);
    }

    public function findAll(){
        return Book::get()->toArray();
    }
}